﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using ZoomImageDemo;
using Joyintech.Model.inpiao365;
using Joyintech.BLL.inpiao365;

namespace Joyintech.Web.inpiao365.UserCenter
{
    public partial class MyHapp_PersonPhoto : System.Web.UI.Page
    {
        private const string savepath = "/pic_upload/userphoto/";

        protected void Page_Load(object sender, EventArgs e)
        {
            customer customer = customer_BLL.GetInstance().GetCurrentCustomer();
            if (customer == null)
            {
                Response.Redirect("UserLogin.aspx");
            }
            if (Page.IsPostBack) return;

            if (!string.IsNullOrEmpty(Request.QueryString["Picurl"]))
            {
                ImageDrag.ImageUrl = Request.QueryString["Picurl"];
                ImageIcon.ImageUrl = Request.QueryString["Picurl"];
                Page.ClientScript.RegisterStartupScript(typeof(MyHapp_PersonPhoto), "step2", "<script type='text/javascript'>Step2();</script>");
            }
            else
            {
                if (!string.IsNullOrEmpty(customer.CustomerLogo))
                {
                    imgphoto.ImageUrl=customer.CustomerLogo;
                }
                
                Page.ClientScript.RegisterStartupScript(typeof(MyHapp_PersonPhoto), "step1", "<script type='text/javascript'>Step1();</script>");
            }
        }

        /// <summary>
        /// 上传图片事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btnUpload_Click(object sender, EventArgs e)
        {
            if (fuPhoto.PostedFile != null && fuPhoto.PostedFile.ContentLength > 0)
            {

                string ext = System.IO.Path.GetExtension(fuPhoto.PostedFile.FileName).ToLower();
                if (ext != ".jpg" && ext != ".jepg" && ext != ".bmp" && ext != ".gif")
                {
                    return;
                }
                string filename = "xuanye_" + DateTime.Now.ToString("yyyyMMddHHmmss") + ext;
                string path = "/pic_upload/userphoto/" + filename;
                fuPhoto.PostedFile.SaveAs(Server.MapPath(path));
                Response.Redirect("/UserCenter/MyHapp_PersonPhoto.aspx?Picurl=" + Server.UrlEncode(path));
            }
            else
            {
                //TODO 
            }
        }

        /// <summary>
        /// 上传剪裁图片事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btn_Image_Click(object sender, EventArgs e)
        {
            string delPic = Server.MapPath(ImageDrag.ImageUrl);

            int imageWidth = Int32.Parse(txt_width.Text);
            int imageHeight = Int32.Parse(txt_height.Text);
            int cutTop = Int32.Parse(txt_top.Text);
            int cutLeft = Int32.Parse(txt_left.Text);
            int dropWidth = Int32.Parse(txt_DropWidth.Text);
            int dropHeight = Int32.Parse(txt_DropHeight.Text);

            string filename = CutPhotoHelp.SaveCutPic(Server.MapPath(ImageIcon.ImageUrl), Server.MapPath(savepath), 0, 0, dropWidth,
                                    dropHeight, cutLeft, cutTop, imageWidth, imageHeight);

            this.imgphoto.ImageUrl = savepath + filename;
            System.IO.File.Delete(delPic);

            customer customer = customer_BLL.GetInstance().GetCurrentCustomer();
            customer.CustomerLogo = imgphoto.ImageUrl;
            customer_BLL.GetInstance().UpdateAutoCheck(customer);
            
            //向Memcached中存储更新SessionId对应的当前登录会员实体
            customer_BLL.GetInstance().StoreCurrentCustomer(HttpContext.Current.Session.SessionID, customer);

            Page.ClientScript.RegisterStartupScript(typeof(MyHapp_PersonPhoto), "step3", "<script type='text/javascript'>Step3();</script>");
        }
    }
}